package ioperations;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-06-01
 * Time: 23:57
 */
public class AddOperation implements Ioperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("增加图书");

        //1.判断书架是否满了
        int currentSize = bookList.getUsedSize();//书本的实际个数
        if(currentSize == bookList.getBooks().length) {
            System.out.println("书架已经放满了");
            return;
        }

        //2.构造对象
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名：");
        String name = scanner.nextLine();

        System.out.println("请输入作者：");
        String author = scanner.nextLine();

        System.out.println("请输入书的类型：");
        String type = scanner.nextLine();

        System.out.println("请输入价格：");
        int price = scanner.nextInt();

        //把这些对象放组装成一本书：
        Book newbook = new Book(name, author, price, type);

        //3.判断这本书是否在书架上，就不能插入了
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookList.getBooks(i);
            //对比：
            if(book1.getName().equals(name)) {
                System.out.println("有这本书就不能插入");
                return;
            }
        }

        //插入这本书：
        bookList.setBooks(currentSize, newbook);
        //书本的实际数目自增1
        bookList.setUsedSize(currentSize + 1);
        System.out.println("新增图书成功！");
    }
}
